package gameObjects;

import java.util.ArrayList;

import player.Player;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;

public class CardTable extends Stage {
	private CardStack stack;
	private CardPile pile;
	private Sprite cardBack;
	private Texture cardBackTexture;
	private ArrayList<Player> playerList = new ArrayList<Player>();
	private Player currentPlayer;

	public CardTable(SpriteBatch _batch) {
		super(480, 800, true);
		cardBackTexture = new Texture(Gdx.files.internal("data/cardback.png"));
		cardBack = new Sprite(cardBackTexture);

		stack = new CardStack(_batch, this);
		pile = new CardPile(this);
		this.addActor(stack);
		this.addActor(pile);

	}

	public void giveCard(Player player) {

		player.addCard(stack.takeTopCard());

	}

	public CardTable(float width, float height, boolean keepAspectRatio) {
		super(width, height, keepAspectRatio);
		// TODO Auto-generated constructor stub
	}

	public CardTable(float width, float height, boolean keepAspectRatio,
			SpriteBatch batch) {
		super(width, height, keepAspectRatio, batch);
		// TODO Auto-generated constructor stub
	}

	public void addCardStack(CardStack _stack) {

	}

	public Sprite getCardBack() {

		return cardBack;
	}

	public void reCalculate() {

		for (int i = 0; i < playerList.size(); i++) {
			Player player = playerList.get(i);
			if (player.getPlayerCards().size() > 3) {
				for (int c = 0; c < player.getPlayerCards().size(); c++) {
					int distance = 480 / player.getPlayerCards().size();
					player.getPlayerCards().get(c)
							.setPosition(c * distance, player.getY());
				}
			} else {
				for (int c = 0; c < player.getPlayerCards().size(); c++) {
					float distance = (480 - cardBack.getWidth()*3)/3 ;
					player.getPlayerCards().get(c)
							.setPosition((c + 1) * distance+(c*cardBack.getWidth()), player.getY());
				}

			}

		}

	}

	public void reShuffleRemainingCards(ArrayList<Card> cardList) {

		stack.reShufflePlayingCards(cardList);

	}

	public int getStackCount() {
		return stack.getStackCount();
	}

	// public void act(float delta) {
	// super.act(delta);
	// pile.act(delta);
	// stack.act(delta);
	// }

	public void addPlayer(Player _player) {

		switch (_player.getPlayerID()) {

		case 0:
			_player.setPosition(0, 25);
			break;
		case 1:
			_player.setPosition(0, this.getHeight() - cardBack.getHeight()-25);
			break;
		case 2:
			break;
		case 3:
			break;
		default:
			break;

		}
		playerList.add(_player);

	}

	public int getCurrentSuit() {

		return pile.getTopSuit();
	}

	public int getCurrentValue() {

		return pile.getTopValue();
	}

	public void playCard(Card _card) {
		boolean cardPlayable = false;
		if (_card.value == 5)
			cardPlayable = true;

		if (_card.value == getCurrentValue()) {

			cardPlayable = true;
		} else {
			if (_card.suit == getCurrentSuit()) {
				cardPlayable = true;
			}
		}
		System.out.println(cardPlayable);
		if (cardPlayable) {

			_card.showBack(false);
			pile.addCard(_card);
			currentPlayer.removeCard(_card);
		}

	}

	public void startGame(int numberPlayers) {
		for (int i = 0; i < numberPlayers; i++) {
			Player player = new Player(i, Integer.toString(i), this);
			for (int c = 0; c < 5; c++) {
				giveCard(player);
			}
			addPlayer(player);

			playerList.add(player);

		}
		currentPlayer = playerList.get(0);
		pile.addCard(stack.takeTopCard());
		reCalculate();
	}

	public Player getCurrentPlayer(){
		return currentPlayer;
	}
	
	
	public void playIt() {
		if (currentPlayer.getPlayerCards().size() > 0) {
			pile.addCard(currentPlayer.getPlayerCards().get(0));
			currentPlayer.removeCard(currentPlayer.getPlayerCards().get(0));
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
